package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcPaymentDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.pojos.CcPayment;
import com.trkj.pojos.CcPrepayment;

@Service
@Transactional
/**
 * 缴费
 * @author Administrator
 *
 */
public class CcPaymentService {

	@Autowired
	private CcPaymentDAO ccPaymentDAO;
	@Autowired
	private CcPrepaymentDAO ccPrepaymentDAO;
	
	public void addccpayment(BigDecimal pmoney,BigDecimal pmStaffid,long ppatientid){
		CcPayment ccPayment=new CcPayment();
		ccPayment.setPmoney(pmoney);
		ccPayment.setPmStaffid(pmStaffid);
		ccPayment.setPaydate(new Date());
		ccPayment.setPpatientid(new BigDecimal(ppatientid));
		ccPaymentDAO.save(ccPayment);
		CcPrepayment cpp=ccPrepaymentDAO.findBypatient(ccPayment.getPpatientid());
		if(cpp!=null){
			cpp.setPpmoney(cpp.getPpmoney().add(pmoney));
			ccPrepaymentDAO.save(cpp);
		}else{
			CcPrepayment cppm=new CcPrepayment();
			cppm.setPpatientno(new BigDecimal(ppatientid));
			cppm.setPpmoney(pmoney);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(new Date());
			cppm.setPpremarks(dateString);
			cppm.setPpstatc("正常");
			ccPrepaymentDAO.save(cppm);
		}
		
	}
}
